package com.ms.user.service.impl;

import com.ms.common.utils.R;
import com.ms.user.util.JwtUtil;
import com.ms.user.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Random;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.common.utils.PageUtils;
import com.ms.common.utils.Query;

import com.ms.user.dao.UserDao;
import com.ms.user.entity.UserEntity;
import com.ms.user.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R que(String phone) {
        QueryWrapper<UserEntity> id = new QueryWrapper<UserEntity>().eq("id", phone);
        UserEntity userEntity = baseMapper.selectOne(id);
        if (userEntity==null){
            return R.ok();
        }
        return R.error("该手机号已被注册");
    }

    @Override
    public R sa(UserEntity userEntity) {
        //获取手机号存入redis中的验证码
        String s = stringRedisTemplate.opsForValue().get(userEntity.getId());
        System.out.println(s);
        if (StringUtils.isEmpty(s)){
            return R.error("验证码已失效,请重新获取验证码");
        }
        System.out.println("从redis中取出的"+s);
        System.out.println("从实体类中取出的"+userEntity.getYzm());
        if (userEntity.getYzm()!=Integer.parseInt(s)){
            return R.error("验证码输入错误");
        }

        // MD5(MD5(pass明文+固定salt)+salt)
//        Random random = new Random();
//        String salt = "";
//        int m =0;//生成0-26的随机数
//        for(int i = 0;i<8;i++){
//            String str="abcdefghijklmnopqrstuvwxyz";
//            m = random.nextInt(26);
//            salt += str.charAt(m);
//        }
//
//        System.out.println("随机生成的密钥:"+salt);
        String s1 = MD5Util.md5Code(userEntity.getPassword(),2);//第一次固定加密
        userEntity.setPassword(s1);
        userEntity.setRegisterData(new Date());
        System.out.println(userEntity);
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<UserEntity>().eq("nickname", userEntity.getNickname());
        UserEntity userEntity1 = baseMapper.selectOne(wrapper);
        if (userEntity1!=null){
            return R.error("该用户名已注册，无需重复注册");
        }
        int insert = baseMapper.inse(userEntity);
        System.out.println(insert);
        if (insert==0){
            return R.error("注册失败");
        }
        return R.ok();
    }

    @Override
    public R login(UserEntity userEntity) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<UserEntity>().eq("nickname", userEntity.getNickname());
        UserEntity userEntity1 = baseMapper.selectOne(wrapper);
        String s = MD5Util.md5Code(userEntity.getPassword(), 2);
        if (userEntity1!=null && userEntity1.getPassword().equals(s)){
            String token = jwtUtil.createJWT(userEntity1.getId() + "", userEntity1.getPassword(), "admin");
            //修改用户最后一次登陆时间
            userEntity1.setLastLoginData(new Date());
            userEntity1.setLoginCount(userEntity1.getLoginCount()+1);
            baseMapper.updateById(userEntity1);

            //返回用户id以及token
            return R.ok().put("token",token).put("uid",userEntity1.getId());
        }
        return R.error("用户名或密码错误");
    }

    @Override
    public String feginGetName(Long uid) {
        QueryWrapper<UserEntity> wa = new QueryWrapper<>();
        QueryWrapper<UserEntity> wrapper = wa.eq("id", uid);
        UserEntity userEntity = baseMapper.selectOne(wrapper);
        return userEntity.getNickname();
    }


}